package cn.tedu.project.oa.vehicle.serveice.Impl;

import cn.tedu.project.oa.asset.mapper.AssetMapper;
import cn.tedu.project.oa.asset.pojo.dto.AssetSaveParam;
import cn.tedu.project.oa.asset.pojo.entity.Asset;
import cn.tedu.project.oa.asset.service.AssetService;
import cn.tedu.project.oa.base.response.PageData;
import cn.tedu.project.oa.vehicle.mapper.VehicleMapper;
import cn.tedu.project.oa.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.project.oa.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.project.oa.vehicle.pojo.entity.Vehicle;
import cn.tedu.project.oa.vehicle.pojo.vo.VehicleVO;
import cn.tedu.project.oa.vehicle.serveice.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
//@Transactional
@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    VehicleMapper vehicleMapper;
    @Autowired
    AssetService assetService;

    @Override
    public PageData selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("查询车辆列表业务:vehicleQuery={}",vehicleQuery);

        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);

        PageData pageData = new PageData();
        pageData.setList(list);

        int num = vehicleMapper.selectCount(vehicleQuery);
        pageData.setTotal(num);

        return pageData;
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("保存车辆业务:vehicleSaveParam={}",vehicleSaveParam);

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        if(vehicle.getId()==null){
            vehicle.setCreateTime(new Date());
            vehicleMapper.insert(vehicle);
            AssetSaveParam assetSaveParam = insetAsset(vehicle);
            assetService.saveAsset(assetSaveParam);
        }else {
            vehicle.setRemark(vehicleSaveParam.getRemark());
            vehicle.setAssetUpdateTime(new Date());
            vehicleMapper.update(vehicle);
            AssetSaveParam assetSaveParam = insetAsset(vehicle);
            assetService.updateAssetByAssetCode(assetSaveParam);
        }
    }

    @Override
    public void deleteVehicle(Long id, String assetCode) {
        log.debug("删除车辆业务:id={},assetCode={}",id,assetCode);

        vehicleMapper.deleteVehicle(id);

        assetService.deleteByAssetCode(assetCode);
    }

    private AssetSaveParam insetAsset(Vehicle vehicle) {
        Asset asset = new Asset();
        BeanUtils.copyProperties(vehicle,asset);
        asset.setCode(vehicle.getAssetCode());
        asset.setBrand(vehicle.getAssetBrand());
        String spec = vehicle.getModel()+ " " + vehicle.getSeat() + "人座";
        asset.setSpecifications(spec);
        asset.setStockNum(1l);
        asset.setBuyTime(vehicle.getAssetBuyTime());
        asset.setPrice(vehicle.getAssetPrice());
        asset.setLocation(vehicle.getAssetLocation());
        asset.setStatus(vehicle.getAssetStatus());
        AssetSaveParam assetSaveParam = new AssetSaveParam();
        BeanUtils.copyProperties(asset,assetSaveParam);

        Integer type = vehicle.getType();
        String typeFormat;
        switch (type){
            case 10:
                typeFormat = "轿车";
                    break;
            case 20:
                typeFormat = "客车";
                    break;
            case 30:
                typeFormat = "货车";
                    break;
            case 40:
                typeFormat = "电单车";
                    break;
            default:
                typeFormat = "其它车辆";
        }
        assetSaveParam.setType(typeFormat);
        return assetSaveParam;
    }
}
